package com.hao.tool.web.haotool;

import cn.hutool.core.lang.Console;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.sax.handler.RowHandler;
import org.junit.Test;

import java.lang.reflect.Field;

/**
 * 读取表格并生成代码
 *
 * @author RuiHaoZ on CMHK.fs
 * @date 2020-05-21 15:59
 */
public class GenerateCodeUtil {

    private static RowHandler createJavaClass() {
        return (sheetIndex, rowIndex, rowlist) -> {
            if (rowlist.size() >= 3 && rowIndex >= 0) {
                String descStr = String.format(" /** %s**/", rowlist.get(1));
//                System.out.println(descStr);
                String fieldStr = String.format("private %s %s;", rowlist.get(2), rowlist.get(0));
//                System.out.println(fieldStr);
                Console.log("{} {}", descStr, fieldStr);

            }
        };
    }

    private static RowHandler createDesc() {
        return (sheetIndex, rowIndex, rowlist) -> {
            if (rowlist.size() > 3 && rowIndex > 1) {
                if (rowIndex == 75) {
                    System.out.println("");
                }
                String desc = rowlist.get(4) == null ? "无" : String.valueOf(rowlist.get(4));
                desc = desc.replaceAll("\n", ";");
                Console.log("{} 、{}: {}", rowIndex - 1, rowlist.get(2), desc);
            }
        };
    }

    private static RowHandler createTableSql() {
        return (sheetIndex, rowIndex, rowlist) -> {
            if (rowlist.size() > 3 && rowIndex > 1) {
                String descStr = String.format(" -- %s", rowlist.get(1));
                String dataType = String.valueOf(rowlist.get(2));
                Object fieldName = rowlist.get(0);
                if (null != fieldName) {
                    if ("String".equals(dataType) || "string".equals(dataType)) {
                        dataType = "varchar(255)";
                    } else if ("Int".equals(dataType) || "Integer".equals(dataType)) {
                        dataType = "int";
                    } else if ("Long".equals(dataType)) {
                        dataType = "bigint";
                    } else {
                        dataType = "varchar(250)";
                    }
                    String fieldStr = String.format(" %s %s,", rowlist.get(0), dataType);
                    Console.log("{} \n{}", descStr, fieldStr);
                }
            }

        };
    }


    private static RowHandler createEnumClass() {
        return (sheetIndex, rowIndex, rowlist) -> {

            if (rowlist.size() > 2 && rowIndex > 0) {
                System.out.println(String.format("CODE_%s(%s, %s, \"%s\"),",
                        rowlist.get(1), rowlist.get(0), rowlist.get(1), rowlist.get(2)));
            }
        };
    }


    @Test
    public void test1() {
        String filePath = "/Users/haol/Desktop/jekou.xlsx";

        ExcelUtil.read07BySax(filePath, 0, createJavaClass());
    }


    @Test
    public void test2() {
        String filePath = "/Users/haol/Downloads/江西流水格式转换V0.1.xlsx";
        System.out.println("create table tb_gantry_record(");
        ExcelUtil.read07BySax(filePath, 2, createTableSql());
        System.out.println(")");
    }


    @Test
    public void test3() {
        for (Field declaredField : RefundInfo.class.getDeclaredFields()) {
            String name = declaredField.getName();
            System.out.println(".set" + name.substring(0, 1).toUpperCase() + name.substring(1) + "(null)");
        }
    }

    @Test
    public void test33() {
        for (Field declaredField : Object.class.getDeclaredFields()) {
            String name = declaredField.getName();
            System.out.println("xx.set" + name.substring(0, 1).toUpperCase() + name.substring(1) + "(null);");
        }
    }

    @Test
    public void test6() {
        for (Field declaredField : Object.class.getDeclaredFields()) {
            String name = declaredField.getName();
            String model = "<result column=\"AAA\"  property=\"AAA\"/>";
            System.out.println(model.replaceAll("AAA",name));
        }
    }

    @Test
    public void test4() {
        String filePath = "/Users/haol/Downloads/t.xlsx";
        ExcelUtil.read07BySax(filePath, 0, createJavaClass());
    }

    @Test
    public void test5() {
        String filePath = "/Users/haol/Documents/Company/手持机/江西/江西流水格式转换V0.3及数据库表定义V0.1-20200708.xlsx";
        ExcelUtil.read07BySax(filePath, 9, createDesc());
    }

}
